package GUIuse.mainuse;

import GUIuse.CustomerPanel;
import GUIuse.OrderPanel;
import GUIuse.ProductPanel;
import common.Customer;
import common.Order;
import common.Product;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class OrderManagementSystem extends JFrame {
    // 数据存储
    private List<Customer> customers = new ArrayList<>();
    private List<Product> products = new ArrayList<>();
    private List<Order> orders = new ArrayList<>();
    
    // 面板
    private CustomerPanel customerPanel;
    private ProductPanel productPanel;
    private OrderPanel orderPanel;
    
    public OrderManagementSystem() {
        // 设置窗口属性
        setTitle("订货单管理系统");
        setSize(1000, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        // 初始化面板
        customerPanel = new CustomerPanel(this);
        productPanel = new ProductPanel(this);
        orderPanel = new OrderPanel(this);
        
        // 创建标签页面板
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("客户管理", customerPanel);
        tabbedPane.addTab("商品管理", productPanel);
        tabbedPane.addTab("订单管理", orderPanel);
        
        // 添加标签页面板到窗口
        add(tabbedPane);
        
        // 添加一些测试数据
        addTestData();
    }
    
    private void addTestData() {
        // 添加测试客户
        customers.add(new Customer(1, "张三商贸有限公司", "张三", "13800138001", "北京市朝阳区", "zhangsan@example.com"));
        customers.add(new Customer(2, "李四科技有限公司", "李四", "13900139001", "上海市浦东新区", "lisi@example.com"));
        
        // 添加测试商品
        products.add(new Product(1, "笔记本电脑", 10, 5999.99));
        products.add(new Product(2, "智能手机", 20, 3999.99));
        products.add(new Product(3, "无线耳机", 50, 899.99));
        
        // 添加测试订单 - 使用第一个客户的ID作为订单的客户ID
        if (!customers.isEmpty()) {
            Order order = new Order("ORD-20230601-001", customers.get(0).getId());
            order.addOrderItem(products.get(0), 2);
            order.addOrderItem(products.get(2), 5);
            orders.add(order);
        }
    }
    
    // 获取客户列表
    public List<Customer> getCustomers() {
        return customers;
    }
    
    // 添加客户
    public void addCustomer(Customer customer) {
        customers.add(customer);
    }
    
    // 更新客户
    public void updateCustomer(Customer updatedCustomer) {
        for (int i = 0; i < customers.size(); i++) {
            if (customers.get(i).getId() == updatedCustomer.getId()) {
                customers.set(i, updatedCustomer);
                break;
            }
        }
    }
    
    // 删除客户
    public void deleteCustomer(int id) {
        customers.removeIf(customer -> customer.getId() == id);
    }
    
    // 获取商品列表
    public List<Product> getProducts() {
        return products;
    }
    
    // 添加商品
    public void addProduct(Product product) {
        products.add(product);
    }
    
    // 更新商品
    public void updateProduct(Product updatedProduct) {
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getId() == updatedProduct.getId()) {
                products.set(i, updatedProduct);
                break;
            }
        }
    }
    
    // 删除商品
    public void deleteProduct(int id) {
        products.removeIf(product -> product.getId() == id);
    }
    
    // 获取订单列表
    public List<Order> getOrders() {
        return orders;
    }
    
    // 添加订单
    public void addOrder(Order order) {
        orders.add(order);
    }
    
    // 更新订单
    public void updateOrder(Order updatedOrder) {
        for (int i = 0; i < orders.size(); i++) {
            if (orders.get(i).getOrderNumber().equals(updatedOrder.getOrderNumber())) {
                orders.set(i, updatedOrder);
                break;
            }
        }
    }
    
    // 删除订单
    public void deleteOrder(String orderNumber) {
        orders.removeIf(order -> order.getOrderNumber().equals(orderNumber));
    }
    
    // 刷新所有面板数据
    public void refreshAllPanels() {
        customerPanel.refreshTable();
        productPanel.refreshTable();
        orderPanel.refreshTable();
    }
    
    public static void main(String[] args) {
        // 在事件调度线程中启动应用程序
        SwingUtilities.invokeLater(() -> {
            OrderManagementSystem system = new OrderManagementSystem();
            system.setVisible(true);
        });
    }
}